<%@ page language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="gae.google.tcserver.db.*"%>
<%@ page import="gae.google.tcserver.api.*"%>
<%@ page import="gae.google.tcserver.model.*"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%!
	private boolean haveActivity(List<Integer> activityDays, int day){
		
		if (activityDays.size() != 0){
			for (int i = 0; i < activityDays.size(); i++){
				if (day == activityDays.get(i).intValue()){
					return true;
				}
			}
		}
		return false;
	}

	private String getActivity(List<Integer> activityDays, List<String> activities, int day){
	
	if (activityDays.size() != 0){
		for (int i = 0; i < activityDays.size(); i++){
			if (day == activityDays.get(i).intValue()){
				return activities.get(i);
			}
		}
	}
	return "";
}

%>
<%
	// days of week
	String[] weekDays = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };

	// holds a list of activities
	List<ActivityBean> activityBeans = new ArrayList<ActivityBean>();
	
	// holds all days with an activity
	List<Integer> activityDays = new ArrayList<Integer>();

	// holds the activity text for all days with an activity
	List<String> activities = new ArrayList<String>();
	String activityText = null;

	// number of activities
	int noOfActivities = 0;

	// get Json
	CalendarBean calendarBean = (CalendarBean) request.getAttribute("calendar");
    int iTotalWeeks = calendarBean.getTotalWeeksInMonth();
    int weekStartDay = calendarBean.getWeekStartDay();
    int days = calendarBean.getNoOfDaysInMonth();
    int weekYear = calendarBean.getWeekYear();
    Date fromDate;
    Date toDate;
    
    String sFromDate = Integer.toString(calendarBean.getYear());
    sFromDate += ".";
    sFromDate += Integer.toString(calendarBean.getMonth() + 1);
    sFromDate += ".01";

    String sToDate = Integer.toString(calendarBean.getYear());
    sToDate += ".";
    sToDate += Integer.toString(calendarBean.getMonth() + 1);
    sToDate += "." + Integer.toString(days);
    
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
	try{
		fromDate = formatter.parse(sFromDate);
		toDate = formatter.parse(sToDate);
	}catch (Exception e){
		fromDate = null;
		toDate = null;
	}
	
	// get json
	CreateJson createJson = new CreateJson();
	String json = createJson.getActivities(fromDate, toDate);
	
	// did we get a json
	if (json != null) {
		// yes, parse json to get all polls
		ParseJson jsonParser = new ParseJson();
		activityBeans =  jsonParser.parseActivities(json);
		if (activityBeans != null) {
			noOfActivities = activityBeans.size();
			for (int i = 0; i < noOfActivities; i++){
			
				ActivityBean activityBean = activityBeans.get(i);
				Date tempDate = new Date(activityBean.getDate());
				DateFormat f = new SimpleDateFormat("d");
				try {
					String day = null;
					day = f.format(tempDate);
					activityDays.add(Integer.valueOf(day));
					activities.add(activityBean.getActivity());
				} catch (Exception e) {
					
				}
			}
		}
	}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="../JS/jquery-ui-1.8.4.custom/js/jquery-1.4.2.min.js"></script>
<link type="text/css" href="../CSS/TeamCalendar.css" rel="stylesheet" />

<title>Calendar</title>

<!-- Clear Activity -->
<script type="text/javascript">
  $(function() {
	$('.activityButtonClear').click(function() {
		document.forms["form"].elements["activity"].value="";
	});
  });

  $(function() {
	$('.activityButtonCancel').click(function() {
		document.getElementById('activity').style.display='none';
	});
  });

  $(function() {
	$('.activityButtonSave').click(function() {
		document.form.submit();
	});
  });
  
function activity(day){
	
	var date = <%=Integer.toString(calendarBean.getYear())%> + ".";
	date += <%=Integer.toString(calendarBean.getMonth() + 1)%> + ".";
	date += day;
	document.form.date.value = date;
	document.getElementById('activity').style.display='block';
}
function previousMonth(m, y){

	if (m == 0){
		m = 11;
		y--;
	}else{
		m--;
	}
	document.frm1.year.value = y;
	document.frm1.month.value = m;
  	document.frm1.submit();
}

function nextMonth(m, y){

	if (m == 11){
		m = 0;
		y++;
	}else{
		m++;
	}
	document.frm1.year.value = y;
	document.frm1.month.value = m;
  	document.frm1.submit();
}

</script>
</head>

<body class="body">

	<div class="mainContainer">
    	
        <div class="topContainer">
        	<img src="../PICS/TeamCalendar.jpg" style="height: 112px; width: 1100px"/> 
        </div>
        
        <div class="mainMenuContainer">
            <a href="/views/TeamCalendar.jsp">Home</a>
           	<a href="#" onclick="document.getElementById('schema').style.display='none';document.getElementById('list').style.display='block';">List</a>
           	<a href="#" onclick="document.getElementById('list').style.display='none'; document.getElementById('schema').style.display='block';">Schema</a>
        </div>

		<div class="calendarContainer">
	       	<div class="calendarName">
	       		<p>Calendar</p>
	       	</div>
	       	<div class="calendarMonthYear">
	       		<a class="previousMonth" href="#" onclick="previousMonth('<%=calendarBean.getMonth()%>','<%=calendarBean.getYear()%>');">&lt;</a>
	       		<p class="monthYear"><%=calendarBean.getMonthName()%> <%=Integer.toString(calendarBean.getYear())%></p>
	       		<a class="nextMonth" href="#" onclick="nextMonth('<%=calendarBean.getMonth()%>','<%=calendarBean.getYear()%>');">&gt;</a>
	       	</div>
	        <div class="calendarForm" id="schema">
				<form name="frm" method="post">
					<input type="hidden" name="year" value=""/>
					<input type="hidden" name="month" value=""/>
					<table class="calendarTable" border="0" cellspacing="0" cellpadding="0">
					  <tr>
					    <td><table align="center" border="1" cellpadding="3" cellspacing="0" width="440px">
					      <tbody>
					        <tr>
					          <th>Mon</th>
					          <th>Tue</th>
					          <th>Wed</th>
					          <th>Thu</th>
					          <th>Fri</th>
					          <th>Sat</th>
					          <th>Sun</th>
					        </tr>
					        <%
					        int cnt = 1;
					        for(int i = 1; i <= iTotalWeeks; i++)
					        {
							%>
					        <tr>
					          <% 
						        for(int j = 1; j <= 7; j++)
						        {
							        if(cnt < weekStartDay || (cnt - weekStartDay + 1) > days)
							        {
							        %>
					                <td align="center" height="60px" width="60px" bgcolor="#E8E8E8">&nbsp;</td>
					                <% 
							        }
							        else if (cnt == 7 || cnt == 14 || cnt == 21 || cnt == 28 || cnt == 35) {
							        	if (haveActivity(activityDays, cnt - weekStartDay + 1)){
							        	%>
					                		<td align="center" height="60px" width="60px" bgcolor="#FF6633"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt - weekStartDay + 1)%>);"><span><%=(cnt - weekStartDay + 1)%></span></a></td>
							        	<%
							        	}else{
							        	%>
							        	    <td align="center" height="60px" width="60px" bgcolor="#FFE6E6"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt - weekStartDay + 1)%>);"><span><%=(cnt - weekStartDay + 1)%></span></a></td>
							        	<%
							        	}
							        }else{
							        	if (haveActivity(activityDays, cnt - weekStartDay + 1)){
							        	%>
					                		<td align="center" height="60px" width="60px" bgcolor="#99CCFF"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt - weekStartDay + 1)%>);"><span><%=(cnt - weekStartDay + 1)%></span></a></td>
										<%
										}else{
										%>
					                		<td align="center" height="60px" width="60px" bgcolor="#FFFFFF"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt - weekStartDay + 1)%>);"><span><%=(cnt - weekStartDay + 1)%></span></a></td>
					                	<%
										}
							        }
							        cnt++;
							    }
						        %>
					        </tr>
					        <%
						    }
						    %>
					      </tbody>
					    </table></td>
					  </tr>
					</table>
				</form>
			</div>
	        <div class="calendarListForm" id="list">
				<form name="frm1" method="post">
					<input type="hidden" name="year" value=""/>
					<input type="hidden" name="month" value=""/>
				   	<div class="calendarWeek">
					  <tr>
					    <td><table class="calendarTable" border="1" align="center" cellpadding="0" cellspacing="0">
					      <tbody>
					        <tr>
					          <th height="25px" width="65px">Week</th>
					        </tr>
					        <% 
					        int weekHight = 0;
					        for(int i = 1; i <= iTotalWeeks; i++){
					        	
					        	if (i == 1){
					        		weekHight = ((7 - (weekStartDay - 1)) * 25);
					        	}else if (i == iTotalWeeks){
									weekHight = ((7 - ((iTotalWeeks * 7) - ((weekStartDay - 1) + (days)))) * 25);		        		
					        	}else{
					        		weekHight = 175;
					        	}
					        	%>
					        	<tr>
					        		<td align="center" height="<%=weekHight%>" width="65px" bgcolor="#FFFFFF"><span><%=weekYear%></span></td>
					        	</tr>
					       	    <%
					       		weekYear++;
					        }
					        %>
					      </tbody>
					    </table></td>
				  	  </tr>
					</div>
				 </form>
			   	 <div class="calendarDayActivity">
					<table border="0" cellspacing="0" cellpadding="0">
					  <tr>
					    <td><table align="center" border="1" cellpadding="0" cellspacing="0">
					      <tbody>
					        <tr>
					          <th height="25px" width="65px">Day</th>
					          <th height="25px" width="300px">Activity</th>
					        </tr>
					        <%
					        int cnt1 = 1;
					        for(int i = 1; i <= iTotalWeeks; i++)
					        {
						        for(int j = 1; j <= 7; j++)
						        {
						        	%>
						        	<tr>
						        	<%
							        if(cnt1 < weekStartDay || (cnt1 - weekStartDay + 1) > days)
							        {
							        }
							        else if (cnt1 == 7 || cnt1 == 14 || cnt1 == 21 || cnt1 == 28 || cnt1 == 35) {
							        	if (haveActivity(activityDays, cnt1 - weekStartDay + 1)){
					                  		activityText = getActivity(activityDays, activities,(cnt1 - weekStartDay + 1));
							        	    %>
							                <td align="center" height="25px" width="65px" bgcolor="#FF6633"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay + 1)%>);"><span><%=weekDays[j - 1]%> <%=(cnt1 - weekStartDay + 1)%></span></a></td>
						                	<td align="center" height="25px" width="300px" bgcolor="#FF6633"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay + 1)%>);"><span><%=activityText%></span></a></td>
						                    <%
							       		}else{
							       		    %>
							                <td align="center" height="25px" width="65px" bgcolor="#FFE6E6"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay + 1)%>);"><span><%=weekDays[j - 1]%> <%=(cnt1 - weekStartDay + 1)%></span></a></td>
						                	<td align="center" height="25px" width="300px" bgcolor="#FFE6E6"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay +1)%>);"><span>&nbsp;</span></a></td>
						                    <%
						                }	
								    }else{
							        	if (haveActivity(activityDays, cnt1 - weekStartDay + 1)){
					                  		activityText = getActivity(activityDays, activities,(cnt1 - weekStartDay + 1));
							        	    %>
							                <td align="center" height="25px" width="65px" bgcolor="#99CCFF"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay + 1)%>);"><span><%=weekDays[j - 1]%> <%=(cnt1 - weekStartDay + 1)%></span></a></td>
							                <td align="center" height="25px" width="300px" bgcolor="#99CCFF"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay + 1)%>);"><span><%=activityText%></span></a></td>
							                <%
					                    }else{
					                        %>
							                <td align="center" height="25px" width="65px" bgcolor="#FFFFFF"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay + 1)%>);"><span><%=weekDays[j - 1]%> <%=(cnt1 - weekStartDay + 1)%></span></a></td>
							                <td align="center" height="25px" width="300px" bgcolor="#FFFFFF"><a class="activityDayCell" href="#" onclick="activity(<%=(cnt1 - weekStartDay +1)%>);"><span>&nbsp;</span></a></td>
							                <%
					                    }
							        }
							        cnt1++;
							        %>
							        </tr>
							        <%
							    }
						    }
						    %>
				      	 </tbody>
				       </table></td>
				     </tr>
				   </table>
				 </div>
			</div>
		</div>
		<!--Calendar Activity-->
        <div class="activityContainer">
        	
            <div class="activityContent">
            
                <div class="activityHeader" id="activity">
                	<div class="activityName">Activity</div>
                	
                    <form name="form" method="post" action="/Activity.do">
						<input type="hidden" name="date" value=""/>
                    	<div><textarea class="activityInput" tabindex="2" name="activity"></textarea></div><br></br>
                    </form>
                    <div class="activityButtons">
                    	<div class="activityButtonClear"><img src="../PICS/buttonClear.png"></img></div>
                    	<div class="activityButtonSave"><img src="../PICS/buttonSave.png"></img></div>
                   		<div class="activityButtonCancel"><img src="../PICS/buttonCancel.png"></img></div>
                    </div>
               </div>
            </div>
		 </div>
		 <form name="form1" method="post" action="/Calendar.do">
			<input type="hidden" name="year" value=""/>
			<input type="hidden" name="month" value=""/>
  		 </form>
	  </div>
</body>
</html>
